import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  {
    path: '/import',
    component: Layout,
    hidden: true, // 隐藏在左侧菜单中
    children: [{
      path: '', // 二级路由path什么都不写 表示二级默认路由
      component: () => import('@/views/import')
    }]
  },
  // { path: '*', redirect: '/404', hidden: true } 
]
// 动态路由
export const asyncRoutes = [
  approvalsRouter,
  departmentsRouter,
  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  settingRouter,
  socialRouter
]
// 创建路由实例的函数
const createRouter = () => new Router({
  // mode:'history', // 可选值有hash和history 默认是hash。
  // hash 路径带有#号 ； history不带#号
  // 注意点： history虽然不带#号，看起来和正常的网址路径一样。但是上线之后，需要和服务器配合，防止变成真正的服务器端路径
  // 建议写 hash模式去交付，如果后端要求该history，打包之后交给他做 服务路径配置
  mode:'hash',
  //hash模式 ： #后面是路由路径，特点是前端访问，#后面的变化不会经过服务器
  //history模式：正常的/访问模式，特点是后端访问，任意地址的变化都会访问服务器
  scrollBehavior: () => ({ y: 0 }),
  // 静态路由+ 动态路由
  // routes: [...constantRoutes, ...asyncRoutes]
  // 静态路由
  routes: [...constantRoutes]
})
// 创建路由实例 
const router = createRouter()
// 重置路由实例
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
// 暴露路由实例
export default router
